package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import persistency.controller.CourseController;
import persistency.controller.OfferedCourseController;
import persistency.controller.TeacherController;

import model.Course;
import model.OfferedCourse;
import model.Teacher;
import model.enums.Classroom;
import util.TimeBlock;
import util.enums.CourseDay;
import controller.PlanningController;
import controller.StartupController;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PlanningFrame extends JFrame {
	
	/* Required for serializable interface */
	private static final long serialVersionUID = 2553440634919690139L;
	
	// INSTANCE VARIABLES
	
	private StartupController startupController;
	private PlanningController planningController;
	private String course;
	private String teacher;
	private String classroom;
	private String dag;
	private String tijdstip;
	private JLabel lblPlanned, lblTeacherPreference;
	private JLabel lblStatusPlanning;
	private JScrollPane jScrollPane1;
	private JScrollPane jScrollPane2;
	private JButton btnRegistration;
	private JComboBox cboCourses, cboTeachers, cboClassrooms, cboDays, cboHours, cboNumberOfPlaces;
	private JTextArea txtPlanned  = new JTextArea(10,50);
	JTextArea txtTeacherPreference = new JTextArea(10,50);
	// CONSTRUCTORS 
	
	public PlanningFrame(StartupController startupController, PlanningController pc) {
		
		// Create frame
		super("Planning cursussen schooljaar 2011-2012");
		
		// Set controllers
		this.startupController = startupController;
		this.planningController = pc;
		
		
		final java.awt.Container container = getContentPane();
		container.setBackground(Color.pink);
		container.setLayout(null);
		
		
		
		this.cboCourses = new JComboBox(CourseController.instance().getAll().toArray());
		JLabel lblCourses = new JLabel("Select course:");
		lblCourses.setBounds(20, 20, 300, 30);
		cboCourses.setBounds(20, 50, 300, 30);
		
		//cursusoverzicht
		this.lblPlanned = new JLabel(getReedsGeplandTekst());
		this.lblPlanned.setBounds(400,20,500,30);
		
		
		txtPlanned.setText(pc.getPlannedCoursesText((Course)cboCourses.getSelectedItem()));
		txtPlanned.setLineWrap(true);
		txtPlanned.setBounds(400,50,500,100);
		txtPlanned.setEditable(false);
		
		
		
		
		JEditorPane pane = new JEditorPane();
		pane.setEditable(true); // Editable
		getContentPane().add(new JScrollPane(pane), "Center");
		
		this.cboTeachers = new JComboBox(TeacherController.instance().getAll().toArray());
		JLabel lblTeachers = new JLabel("Select teacher:");
		lblTeachers.setBounds(20, 150, 300, 30);
		this.cboTeachers.setBounds(20, 180, 300, 30);
		
		//lectorenoverzicht
		this.lblTeacherPreference = new JLabel(getLectorVoorkeurTekst());
		this.lblTeacherPreference.setBounds(400,150,500,30);
		
		txtTeacherPreference.setText(pc.getPreferedCoursesText((model.Teacher)cboTeachers.getSelectedItem()));
		txtTeacherPreference.setBounds(400,180,500,100);
		txtTeacherPreference.setEditable(false);
		

		this.cboClassrooms = new JComboBox(Classroom.values());
		JLabel lblClassrooms = new JLabel("Selecteer classroom:");
		lblClassrooms.setBounds(20, 280, 300, 30);
		this.cboClassrooms.setBounds(20, 310, 300, 30);
		
		
		
		this.cboDays = new JComboBox(CourseDay.values());
		this.cboDays.setMaximumRowCount(6);
		JLabel lblDay = new JLabel("Select day:");
		lblDay.setBounds(20, 350, 200, 30);
		this.cboDays.setBounds(20, 380, 100, 30);
		
		//aanvangsuur cursus - Een cursus kan starten op het uur of het haluur - dynamisch aanpassen aan lesdag
		String [] hours = { "17:00","17:30","18:00","18:30","19:00","19:30","20:00" };
		this.cboHours = new JComboBox(hours);
		this.cboHours.setMaximumRowCount(7);
		JLabel lblHours = new JLabel("Selecteer starting time:");
		lblHours.setBounds(220, 350, 300, 30);
		this.cboHours.setBounds(220, 380, 80, 30);
		
		//aantal beschikbare plaatsen
		
		String [] numberOfPlaces = {"10","15","20","25"};
		this.cboNumberOfPlaces = new JComboBox(numberOfPlaces);
		this.cboNumberOfPlaces.setMaximumRowCount(4);
		JLabel lblNumberOfPlaces = new JLabel("Select number of places:");
		lblNumberOfPlaces.setBounds(420, 350, 300, 30);
		this.cboNumberOfPlaces.setBounds(420, 380, 80, 30);
		
		//registratieknop
		btnRegistration = new JButton(getRegistratieTekst());
		btnRegistration.setBounds(20,440,900,30);
		
        //statusPlanning
		lblStatusPlanning = new JLabel();
		
		lblStatusPlanning.setBounds(20,500,810,30);
		
		container.add(lblCourses);
		container.add(cboCourses);
		container.add(lblTeachers);
		container.add(cboTeachers);
		container.add(lblClassrooms);
		container.add(cboClassrooms);
		container.add(lblDay); container.add(cboDays);
		container.add(lblHours); container.add(cboHours);
		container.add(lblNumberOfPlaces); container.add(cboNumberOfPlaces);
		container.add(lblPlanned); 
		{
			jScrollPane1 = new JScrollPane();
			container.add(jScrollPane1);
			jScrollPane1.setBounds(400, 50, 500, 100);
			jScrollPane1.setViewportView(txtPlanned);
			txtPlanned.setPreferredSize(new java.awt.Dimension(482, 98));
		}
		{
			jScrollPane2 = new JScrollPane();
			container.add(jScrollPane2);
			jScrollPane2.setBounds(400, 180, 500, 100);
			jScrollPane2.setViewportView(txtTeacherPreference);
		}
		container.add(btnRegistration);
		container.add(lblStatusPlanning);

		//eventhandlers
		this.cboCourses.addItemListener(new ItemListener()
		{
			public void itemStateChanged(ItemEvent event){
				btnRegistration.setText(getRegistratieTekst());
				lblPlanned.setText(getReedsGeplandTekst());
				lblTeacherPreference.setText(getLectorVoorkeurTekst());
				txtPlanned.setText(planningController.getPlannedCoursesText((Course)cboCourses.getSelectedItem()));
				container.validate();
			}
		});
		
		this.cboTeachers.addItemListener(new ItemListener()
	    {
			public void itemStateChanged(ItemEvent event){
				btnRegistration.setText(getRegistratieTekst());
				txtTeacherPreference.setText(planningController.getPreferedCoursesText((model.Teacher)cboTeachers.getSelectedItem()));
				container.validate();
			}
	    });
		
		this.cboClassrooms.addItemListener(new ItemListener()
	    {
			public void itemStateChanged(ItemEvent event){
				btnRegistration.setText(getRegistratieTekst());
				container.validate();
			}
	    });
		
		this.cboDays.addItemListener(new ItemListener()
	    {
			public void itemStateChanged(ItemEvent event){
				btnRegistration.setText(getRegistratieTekst());
				container.validate();
			}
	    });
		
		cboHours.addItemListener(new ItemListener()
	    {
			public void itemStateChanged(ItemEvent event){
				btnRegistration.setText(getRegistratieTekst());
				container.validate();
			}
	    });
		
		btnRegistration.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event){
				try{
					OfferedCourse offeredCourse = new OfferedCourse();
					offeredCourse.setClassroom((Classroom)cboClassrooms.getSelectedItem());
					Course course = (Course)cboCourses.getSelectedItem();
					offeredCourse.setCourse(course);
					offeredCourse.setTeacher((Teacher)cboTeachers.getSelectedItem());
					offeredCourse.setNumberOfPlaces(Integer.parseInt(cboNumberOfPlaces.getSelectedItem().toString()));

					TimeBlock tbc = new TimeBlock((CourseDay)cboDays.getSelectedItem(),cboHours.getSelectedItem().toString(), course.getCourseMinutes());
					offeredCourse.setTimeBlock(tbc);
					
					OfferedCourseController.instance().insertOfferedCourse(offeredCourse);
					txtPlanned.setText(planningController.getPlannedCoursesText((Course)cboCourses.getSelectedItem()));
					lblStatusPlanning.setForeground(Color.GREEN);
					lblStatusPlanning.setText("Course planned succesfully!");
					
					
				}catch(Exception e){
					lblStatusPlanning.setForeground(Color.RED);
					lblStatusPlanning.setText(e.getMessage());
					e.printStackTrace();
				}
				
			}
		});
		
		this.addWindowListener(new WindowAdapter()  
		{	          
 			public void windowClosing(WindowEvent e){  
 				setVisible(false);
 				showMenu();
 			}	
		});
		
		this.setSize(1000, 750);
	}
	
	private void showMenu(){
		startupController.execute();
	}
	
	private String getRegistratieTekst(){
		this.course = "" + this.cboCourses.getSelectedItem();
		this.teacher = "" + this.cboTeachers.getSelectedItem();
		this.classroom = "" + this.cboClassrooms.getSelectedItem();
		dag = "" + cboDays.getSelectedItem();
		tijdstip = "" + cboHours.getSelectedItem();
		return "Register: " + " Course: " + this.course + "\n" +
				" Teacher: " + this.teacher +
		        " Classroom: " + this.classroom +
		        " Day: " + dag +
		        " Start time: " + tijdstip;
	}
	
	private String getFilmOverzichtTekst(){
		return "????"+"\n"+"????";
	}
	
	private String getReedsGeplandTekst(){
		return "Overzicht reeds geplande cursussen voor " + cboCourses.getSelectedItem();
	}
	
	private String getLectorOverzichtTekst(){
		return "????"+"\n"+"????";
	}
	
	private String getLectorVoorkeurTekst(){
		return "Overzicht beschikbare lectoren met voorkeur voor " + cboCourses.getSelectedItem();
	}
	
	public static void main (String [] args){
		//PlanningFrame pl = new PlanningFrame(new OpstartController());
		//pl.setVisible(true);
	}
}